﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using RegionInterface;

namespace RegionCore
{
    public class SocketEntry
    {
        public Socket Client;
        public bool HasPolicy = false;
        public PlayerData Player;
        public MessageDomain Domain;
        public byte[] ReceiveBuffer;
        public int ReceiveLength;
        public SocketAsyncEventArgs ReceiveEventArgs;

        //public SocketAsyncEventArgs SendEventArgs;
        //private ManualResetEvent sendLock;
        public SocketEntry(Socket client)
        {
            Client = client;
            ReceiveBuffer = new byte[DataObject.BufferSize];
            ReceiveEventArgs = new SocketAsyncEventArgs();
            ReceiveEventArgs.UserToken = this;
            ReceiveEventArgs.SetBuffer(ReceiveBuffer, 0, ReceiveBuffer.Length);

            //SendEventArgs = new SocketAsyncEventArgs();
            //SendEventArgs.UserToken = this;
            //SendEventArgs.SetBuffer(SendBuffer, 0, SendBuffer.Length);
            //SendEventArgs.Completed += SendCallback;
            //sendLock = new ManualResetEvent(true);
        }
        public void SendData(DataObject data)
        {
            //sendLock.WaitOne();
            //SendEventArgs.SetBuffer(data.Buffer, 0, data.Buffer.Length);
            //SendEventArgs.SetBuffer(0, data.Length);
            //Client.SendAsync(SendEventArgs);
            Client.Send(data.Buffer, data.Length, SocketFlags.None);
        }
        //public void SendCallback(object sender, SocketAsyncEventArgs e)
        //{
        //    sendLock.Set();
        //}
        public void OtherLogout(int id)
        {
            DataObject logoutData = DataPool.Get();
            logoutData.WriteByte(MessageType.LOGOUT);
            logoutData.WriteInt32(id);
            SendData(logoutData);
            DataPool.Put(logoutData);
        }
        public BufferPool DataPool = new BufferPool();
    }

}
